import 'package:flutter/material.dart';
import '../widgets/w_home_left_widget.dart';
import 'package:window_manager/window_manager.dart';

// Windows 端首页
class WindowsHomeScreen extends StatefulWidget {
  const WindowsHomeScreen({super.key});

  @override
  _WindowsHomeScreenState createState() => _WindowsHomeScreenState();
}

// 使用继承而不是混入
class _WindowsHomeScreenState extends State<WindowsHomeScreen>
    with WindowListener {
  @override
  void initState() {
    super.initState();
    // 初始化 window_manager
    _initWindow();
  }

  Future<void> _initWindow() async {
    await windowManager.ensureInitialized();
    // 窗口的配置
    WindowOptions windowOptions = const WindowOptions(
      size: Size(1440, 1024), // 窗口初始化的尺寸
      center: true, // 窗口初始位置
      alwaysOnTop: true, // 窗口指顶
      backgroundColor: Color(0xFFE6EBF2), // 窗口边缘背景色
      skipTaskbar: false, // 任务栏显示应用图标
      titleBarStyle: TitleBarStyle.hidden, // 隐藏window默认的按钮
      windowButtonVisibility: false,
    );
    await windowManager.waitUntilReadyToShow(windowOptions, () async {
      await windowManager.show();
      await windowManager.focus();
      windowManager.setResizable(false); //设置窗口不可缩放
    });
    windowManager.addListener(this);
  }

  @override
  void dispose() {
    windowManager.removeListener(this);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Windows Home')),
      body: const WindowsCustomWidget(),
    );
  }

  @override
  void onWindowClose() async {
    bool isPreventClose = await windowManager.isPreventClose();
    if (isPreventClose) {
      // 处理关闭窗口的逻辑
      print('Window close is prevented.');
    }
  }
}
